package com.fand.platform.configuration.service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.fand.dto.PaginationDto;
import com.fand.platform.configuration.dao.LanguageDao;
import com.fand.platform.configuration.dto.LanguageDto;

/**
 * 语言服务
 *
 * @author yaoHT
 * 
 */
@Getter 
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class LanguageDefaultService implements LanguageService {
	private LanguageDao languageDao;
	
	@Override
	@CacheEvict(value = LanguageService.CACHE_NAME, allEntries = true)
	public void reload() {
	}

	@Override
	@Transactional
	public LanguageDto saveLanguageByIdOrCodeForLanguage(LanguageDto saveLanguage) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveLanguage.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		LanguageDto oldLanguage = this.languageDao.selectLanguageBy1ForMappedObject(LanguageDto.class,
				saveLanguage, null, whereEqProperties);

		if (oldLanguage == null) {
			this.languageDao.insertLanguageBy1(saveLanguage);

			return saveLanguage;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveLanguage, oldLanguage, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.languageDao.updateLanguageBy1(saveLanguage, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveLanguage, oldLanguage, updateProperties);
		}

		return oldLanguage;
	}

	@Override
	@Transactional
	public LanguageDto saveLanguageBy2ForLanguage(LanguageDto saveLanguage) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveLanguage.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		LanguageDto oldLanguage = this.languageDao.selectLanguageBy1ForMappedObject(LanguageDto.class,
				saveLanguage, null, whereEqProperties);

		if (oldLanguage == null) {
			this.languageDao.insertLanguageBy1(saveLanguage, new String[] {});

			return saveLanguage;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveLanguage, oldLanguage, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.languageDao.updateLanguageBy1(saveLanguage, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveLanguage, oldLanguage, updateProperties);
		}

		return oldLanguage;
	}

	@Override
	@Transactional
	public int deleteLanguageBy1(LanguageDto deleteLanguage) {
		return this.languageDao.deleteLanguageBy1(deleteLanguage, new String[] {});
	}

	@Override
	@Transactional
	public int deleteLanguageByIds(LanguageDto deleteLanguage) {
		return this.languageDao.deleteLanguageByIds(deleteLanguage);
	}

	@Override
	@Transactional
	public int setLanguageBy1(LanguageDto setLanguage) {
		return this.languageDao.updateLanguageBy1(setLanguage, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setLanguageBy2(LanguageDto setLanguage) {
		return this.languageDao.updateLanguageBy2(setLanguage);
	}

	@Override
	public Integer readLanguageByCodeNqIdForInteger(LanguageDto readLanguage) {
		return this.languageDao.selectLanguageByCodeNqIdForInteger(readLanguage);
	}

	@Override
	public Integer readLanguageBy2ForInteger(LanguageDto readLanguage) {
		return this.languageDao.selectLanguageBy2ForObject(Integer.class, readLanguage, null);
	}

	@Override
	public List<Integer> readListLanguageBy1(LanguageDto readLanguage) {
		return this.languageDao.selectLanguageBy1ForObjectList(Integer.class, readLanguage, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListLanguageBy2(LanguageDto readLanguage) {
		return this.languageDao.selectLanguageBy2ForObjectList(Integer.class, readLanguage, null);
	}

	@Override
	public LanguageDto getLanguageBy1(LanguageDto getLanguage) {
		return this.languageDao.selectLanguageBy1ForMappedObject(LanguageDto.class, getLanguage, null, "id");
	}

	@Override
	public LanguageDto getLanguageById(LanguageDto getLanguage) {
		return this.languageDao.selectLanguageBy1ForMappedObject(LanguageDto.class, getLanguage, null, "id");
	}

	@Override
	public List<LanguageDto> findLanguageBy1(LanguageDto findLanguage) {
		return this.languageDao.selectLanguageBy1ForMappedObjectList(LanguageDto.class, findLanguage,
				new String[] {}, new String[] {});
	}

	@Override
	public List<LanguageDto> findLanguageBy2(LanguageDto findLanguage) {
		return this.languageDao.selectLanguageBy2ForMappedObjectList(LanguageDto.class, findLanguage);
	}

	@Override
	public PaginationDto<LanguageDto> paginationLanguageBy1ForLanguageList(LanguageDto paginationLanguage) {
		return this.languageDao.paginationLanguageBy1ForMappedObjectList(LanguageDto.class,
				paginationLanguage);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationLanguageBy1ForMapList(LanguageDto paginationLanguage) {
		return this.languageDao.paginationLanguageBy1ForMapList(paginationLanguage);
	}
}